#coding:utf-8
import time

from jinja2 import Markup
from jinja2 import environmentfilter
from jinja2 import escape
from jinja2 import evalcontextfilter
import re
import urllib
__author__ = "hzq"
__date__ = "$2011-2-21 13:13:48$"
from libs import strip_tags
def seturl(types, * arg):
    if types == 'cate':

        tpl = ["/category/%s.html", "/category/%s/page-%s.html"]

        return tpl[len(arg)-1] % arg
    
    elif types == "logview":
        return "/category/%s/log/%s.html" % arg
    elif types == "tags":
        tpl = ["/tags/%s/", "/tags/%s/%s.html"]

        return tpl[len(arg)-1] % arg
    else:
        return " "



def htmleditor(s, ** arg):


    css = arg.pop("css", [])
    form = arg.pop("form", [])
    id=form = arg.pop("id", [])
    str = '<script charset="utf-8" src="/static/edit/kindeditor.js"></script>'
    str += " <script>KE.show({id : '%s',cssPath : '%s'," % (id, css)
    str += '''
				
			     
				afterCreate : function(id) {
					KE.event.ctrl(document, 13, function() {
						KE.sync(id);
						'''
    str += "document.forms['%s'].submit();" % form
    str += '''				});
					KE.event.ctrl(KE.g[id].iframeDoc, 13, function() {
						KE.sync(id);
                                                '''
    str += "document.forms['%s'].submit();" % form
    str += '''				});
				}
			});

   </script>
   <textarea id="%s" name="content1" style="width:700px;height:300px;visibility:hidden;">%s</textarea>
 ''' % (id,s)

    return str
    
def urlencode(s):
    return urllib.quote(s)


def timeformat(t, f):
    return time.strftime(f, time.localtime(t))


def htmlInput(s, ** arg):
    if type(arg) == type({}):
        tmp = ''
        for k in arg.keys():
            tmp += "%s='%s'  " % (k, arg[k])
    return "<input type='%s'  %s />" % (s, tmp)
            

def htmlSelect(name, values, default, ** arg):
    tmp = ''
    for k in arg.keys():
        tmp += "%s='%s'  " % (k, arg[k])
    selects = "<select name='%s' %s>" % (name, tmp)
    tmp = ''
    for k in values.keys():
        if values[k] == default:
            tmp += "<option value='%s' selected='selected'>%s</option>" % (values[k], k)
        else:
            tmp += "<option value='%s' >%s</option>" % (values[k], k)
				
			
    return selects + tmp + "</select>"


def htmlsub(s, lens):

    Arr = s.split("<")

    i = 0
    num = len(Arr)
    len1 = 0
    while len1 < lens  and  i < num:

        len1 = len(strip_tags(Arr[i])) + len1

        i = i + 1

        arr1 = Arr[0:i]
        str = strip_tags("<".join(arr1), 'img,span,br,p')

    preg = r'/<(p|span|br)\s*(.+)\s*([\/]*)>/'
    preg = re.compile(preg, re.I)
    str = preg.sub(r'<\1  \3>', str)

    return str




